// pages/product/component/coupon.js
import {
  create
} from '../../../../api/coupon'
const util = require('../../../../utils/util.js')
let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object
    }
  },
  lifetimes: {
    attached() {
      var _this = this
      console.log(this.data.info)
      _this.data.info.coupons.forEach(function(item) {
        item.endDate = util.datetimeFormat(item.endDate)
      })
      _this.setData({
        info: _this.data.info
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    maskClick(e) {
      if (app.globalData.token == null) {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      } else {
        create({
          couponId: e.currentTarget.dataset.id,
          mchId: app.globalData.mchId
        }).then(res => {
          wx.showToast({
            title: '领取成功',
            icon: 'success',
            duration: 2000
          })
        })
      }
    },
    cancleClick() {
      this.triggerEvent('cancleClick')
    }
  }
})